package com.ebt.m.activity;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.permission.PermissionUtil;
import com.ebt.m.commons.utils.SystemUtil;
import com.ebt.m.commons.widgets.BaseRxActivity;
import com.ebt.m.data.bean.UserInfo;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;


public abstract class BaseToolBarActivity extends BaseRxActivity {
    protected FrameLayout content;
    protected Toolbar toolbar;
    protected int colorPrimary;
    protected int colorPrimaryDark;
    protected int colorAccent;
    private TextView mTitleView;
    protected boolean isPermissionChecked;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(getBasicContentLayout());
        TypedArray array = obtainStyledAttributes(new int[]{R.attr.colorPrimary, R.attr.colorPrimaryDark, R.attr.colorAccent});
        colorPrimary = array.getColor(0, 0xFF1473AF);
        colorPrimaryDark = array.getColor(1, 0xFF11659A);
        colorAccent = array.getColor(2, 0xFF3C69CE);
        array.recycle();
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        mTitleView = (TextView) findViewById(R.id.title);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        content = (FrameLayout) findViewById(R.id.content);
        toolbar.setNavigationOnClickListener(v -> onBackPressed());
        PermissionUtil.getInstance().clearFragmentManagerInsideFragments(this);
        isPermissionChecked = PermissionUtil.getInstance().checkNecessaryPermissions(this);
        if (!isPermissionChecked) {
            restartApp();
            finish();
            return;
        }
    }

    private void restartApp() {
        Context context = AppContext.getInstance();
        Intent launchIntent = new Intent(context, IndexActivity.class);
        launchIntent.setFlags(
                Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(launchIntent);
    }

    @Override
    public void setContentView(int layoutResID) {
        content.removeAllViews();
        getLayoutInflater().inflate(layoutResID, content);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (this instanceof LoginActivity) {

        } else {
            UserInfo userInfo = AppContext.getCurrentUser();
            if (!userInfo.isLogined()) {
                Intent intent = new Intent(AppContext.getInstance(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                AppContext.getInstance().startActivity(intent);
            }
        }
    }

    public void setContentViewSuper(int layoutResID) {
        super.setContentView(layoutResID);
    }

    @Override
    public void setContentView(View view) {
        content.removeAllViews();
        content.addView(view);
    }

    public void setContentViewUseSuper(View view) {
        super.setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        content.removeAllViews();
        content.addView(view, params);
    }

    public void setContentViewsSuper(View view, ViewGroup.LayoutParams params) {
        super.setContentView(view, params);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected int getBasicContentLayout() {
        return R.layout.activity_basetoolbar;
    }

    /**
     * 修改返回按钮
     *
     * @param id
     */
    protected void setNaviIcon(int id) {
        toolbar.setNavigationIcon(id);
    }

    public void setTitle4Js(CharSequence title) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTitleView.setVisibility(View.VISIBLE);
                mTitleView.setText(title);
                if (title != null && !"".equals(title)) {
                    toolbar.setVisibility(View.VISIBLE);
                } else {
                    toolbar.setVisibility(View.GONE);
                }
            }
        });

    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleView.setVisibility(View.VISIBLE);
        mTitleView.setText(title);
    }

    @Override
    public void setTitle(int titleId) {
        mTitleView.setVisibility(View.VISIBLE);
        mTitleView.setText(titleId);
    }

    public String getBaseTitle() {
        return mTitleView.getText().toString().trim();
    }

    public String getStr(@StringRes int resId) {
        return getResources().getString(resId);
    }

    public void gotoActivity(Class<?> cls) {
        Intent intent;
        intent = new Intent(this, cls);
        startActivity(intent);
    }

    public void gotoActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }


    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (isPermissionChecked)
            init();
    }

    protected void init() {

    }

    /**
     * 关闭键盘
     */
    public void closeInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && this.getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
